//
//  PlanDetailsViewController.m
//  FitTime
//
//  Created by dllo on 16/4/3.
//  Copyright © 2016年 赵建平. All rights reserved.
//

#import "PlanDetailsViewController.h"
const CGFloat BackGroupHeight = 200;
#import "SectionOneTableViewCell.h"
#import "ChildDetailedViewController.h"
#import "ChildPlanDetailsViewController.h"
#import "ChildTrendsViewController.h"


@interface PlanDetailsViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>

@property (nonatomic, strong) UIImageView *headImageView;//上方图片
@property (nonatomic, strong) UILabel *titleLable;//标题
@property (nonatomic, strong) UILabel *toolLable;//器械
@property (nonatomic, strong) UILabel *trainLable;//训练次数
@property (nonatomic, strong) UIView *waterView;//难度
@property (nonatomic, strong) UILabel *difficultLable;//难度
@property (nonatomic, strong) UIView *downView;

@property (nonatomic, strong) UIButton *planButton;
@property (nonatomic, strong) UIButton *detailButton;
@property (nonatomic, strong) UIButton *trendsButton;
@property (nonatomic, strong) UIView *longView;
@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UILabel *topicLable;
@property (nonatomic, strong) NSString *topicSring;
@property (nonatomic, strong) ChildPlanDetailsViewController *planDetailsVC;
@end

@implementation PlanDetailsViewController

- (void)viewWillAppear:(BOOL)animated {
    
    self.navigationController.navigationBar.barTintColor = [UIColor clearColor];
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBar.layer.borderColor = [UIColor whiteColor].CGColor;
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
    
}

- (void)viewWillDisappear:(BOOL)animated {
    
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setBackgroundImage:[self imageWithColor:[[UIColor whiteColor]colorWithAlphaComponent:1]]forBarMetrics:UIBarMetricsDefault];
    
}




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"return"] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
    
    self.navigationItem.leftBarButtonItem = left;
    self.topicLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
    self.topicLable.textAlignment = NSTextAlignmentCenter;
    self.topicLable.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = self.topicLable;
 
    [self createTableView];
    
}

#pragma mark - 创建tableView
- (void)createTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT) style:UITableViewStylePlain];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.contentInset = UIEdgeInsetsMake(SCREENHEIGHT / 3, 0, 0, 0);
    [self createDownView];
    self.tableView.tableFooterView = self.downView;
    
    [self.tableView registerClass:[SectionOneTableViewCell class] forCellReuseIdentifier:@"SectionOneTableViewCell"];
    
    [self.view addSubview:self.tableView];
    [self createHeadImageView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SectionOneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SectionOneTableViewCell"];
    
    NSString *photoString1 = [NSString stringWithFormat:@"http://ft-video.fit-time.cn/program_%@.png@!480", _videoModel.ID];
    [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:photoString1]];
    cell.nameLable.text = self.videoModel.coachName;
    cell.introduceLable.text = self.videoModel.coachDesc;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 80;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}



#pragma mark - 创建顶部视图
- (void)createHeadImageView {
    //头像
    self.headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -SCREENHEIGHT / 3, SCREENWIDTH, SCREENHEIGHT / 3)];
    
    [self.tableView addSubview:self.headImageView];
    
    //训练次数
    self.trainLable = [[UILabel alloc] init];
    [self.headImageView addSubview:self.trainLable];
    
    self.toolLable = [UILabel new];
    [self.headImageView addSubview:self.toolLable];
    
    [self.trainLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.headImageView.mas_right).offset(-20);
        make.bottom.equalTo(self.headImageView.mas_bottom).offset(-30);
        make.height.equalTo(@20);
    }];
    
    [self.toolLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.headImageView.mas_right).offset(-20);
        make.bottom.equalTo(self.trainLable.mas_top).offset(-10);
        make.height.equalTo(@20);
    }];
    
    
    self.waterView = [UIView new];
    [self.headImageView addSubview:self.waterView];
    
    [self.waterView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.toolLable.mas_left).offset(-15);
        make.bottom.equalTo(self.trainLable.mas_top).offset(-10);
        make.height.equalTo(@20);
        make.width.equalTo(@45);
    }];
    
    self.difficultLable = [UILabel new];
    [self.headImageView addSubview:self.difficultLable];
    
    [self.difficultLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.waterView.mas_left).offset(-10);
        make.bottom.equalTo(self.trainLable.mas_top).offset(-10);
        make.height.equalTo(@20);
    }];
    
    self.titleLable = [UILabel new];
    [self.headImageView addSubview:self.titleLable];
    
    [self.titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.headImageView.mas_right).offset(-20);
        make.bottom.equalTo(self.difficultLable.mas_top).offset(-10);
        make.height.equalTo(@30);
    }];
    
    self.trainLable.textColor = [UIColor whiteColor];
    self.trainLable.font = [UIFont systemFontOfSize:15 weight:1];

    self.toolLable.textColor = [UIColor whiteColor];
    self.toolLable.font = [UIFont systemFontOfSize:15 weight:1];

    self.difficultLable.textColor = [UIColor whiteColor];
    self.difficultLable.font = [UIFont systemFontOfSize:15 weight:1];
    
    self.titleLable.textColor = [UIColor whiteColor];
    self.titleLable.font = [UIFont systemFontOfSize:20 weight:2];
    
    [self createFuZhi];
}

#pragma mark - 创建尾视图
- (void)createDownView {
    
    self.downView = [[UIView alloc] initWithFrame:CGRectMake(0, 150, SCREENWIDTH, SCREENHEIGHT - 150)];
    
    self.planButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_planButton addTarget:self action:@selector(planButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.downView addSubview:_planButton];
    
    /*
    self.trendsButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_trendsButton addTarget:self action:@selector(trendsButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.downView addSubview:_trendsButton];
    */
     
    self.detailButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_detailButton addTarget:self action:@selector(detailButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.downView addSubview:_detailButton];
    
    self.longView = [[UIView alloc] init];
    [self.downView addSubview:self.longView];
    
    [self.planButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.downView.mas_top);
        make.left.equalTo(self.downView.mas_left);
        make.width.equalTo(@(SCREENWIDTH / 2));
        make.height.equalTo(@40);
    }];
    
    [self.longView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.planButton.mas_centerY).offset(17);
        make.centerX.equalTo(self.planButton.mas_centerX);
        make.width.equalTo(@(SCREENWIDTH / 3));
        make.height.equalTo(@3);
    }];
    
    /*
    [self.trendsButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.downView.mas_top);
        make.left.equalTo(self.planButton.mas_right);
        make.width.equalTo(@(SCREENWIDTH / 3));
        make.height.equalTo(@40);
    }];
     */
    
    [self.detailButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.downView.mas_top);
        make.left.equalTo(self.planButton.mas_right);
        make.width.equalTo(@(SCREENWIDTH / 2));
        make.height.equalTo(@40);
    }];
    
    [self.planButton setTitle:@"计划" forState:UIControlStateNormal];
    /*
    [self.trendsButton setTitle:@"动态" forState:UIControlStateNormal];
     */
    [self.detailButton setTitle:@"详情" forState:UIControlStateNormal];
    
    [self.planButton setBackgroundColor:[UIColor colorWithRed:110 / 255.0 green:110 / 255.0 blue:110 / 255.0 alpha:0.4]];
    
    /*
    [self.trendsButton setBackgroundColor:[UIColor colorWithRed:110 / 255.0 green:110 / 255.0 blue:110 / 255.0 alpha:0.4]];
     */
    
    [self.detailButton setBackgroundColor:[UIColor colorWithRed:110 / 255.0 green:110 / 255.0 blue:110 / 255.0 alpha:0.4]];
    self.longView.backgroundColor = [UIColor redColor];
    
    [self createScrollView];
    
}

#pragma mark - 计划按钮点击事件
- (void)planButton:(UIButton *)plan {
    
    [UIView transitionWithView:self.longView duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
        
    self.longView.frame = CGRectMake((SCREENWIDTH / 2 - SCREENWIDTH / 3) / 2, 37, SCREENWIDTH / 3, 3);
    [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];

    } completion:^(BOOL finished) {
        
    }];
}

/*
#pragma mark - 动态按钮点击事件
- (void)trendsButton:(UIButton *)plan {
    
    [UIView transitionWithView:self.longView duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
        
        self.longView.frame = CGRectMake(SCREENWIDTH / 3 +(SCREENWIDTH / 3 - SCREENWIDTH / 4) / 2, 37, SCREENWIDTH / 4, 3);
        [self.scrollView setContentOffset:CGPointMake(SCREENWIDTH, 0) animated:YES];

    } completion:^(BOOL finished) {
        
    }];
}
*/
 
#pragma mark - 详情按钮点击事件
- (void)detailButton:(UIButton *)plan {
    [UIView transitionWithView:self.longView duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
        
        self.longView.frame = CGRectMake(SCREENWIDTH * 1 / 3 +(SCREENWIDTH / 2 - SCREENWIDTH / 3) / 2 * 3, 37, SCREENWIDTH / 3, 3);
        [self.scrollView setContentOffset:CGPointMake(SCREENWIDTH, 0) animated:YES];
    } completion:^(BOOL finished) {
    }];
}

#pragma mark -创建下边的scrollView
- (void)createScrollView {
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 40, SCREENWIDTH, self.downView.bounds.size.height)];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.contentSize = CGSizeMake(SCREENWIDTH * 2, 0);
    self.scrollView.delegate = self;
    
    [self.downView addSubview:self.scrollView];
    [self createChildrenVC];
    
}

#pragma mark - 创建子视图控制器
- (void)createChildrenVC {
    
    self.planDetailsVC = [[ChildPlanDetailsViewController alloc] init];
    _planDetailsVC.view.frame = CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT);
    
    _planDetailsVC.IDArray = self.IDArray;

    [self addChildViewController:_planDetailsVC];
    [self.scrollView addSubview:_planDetailsVC.view];
    
  /* //动态
    
    ChildTrendsViewController *trendsVC = [[ChildTrendsViewController alloc] init];
    trendsVC.view.frame = CGRectMake(SCREENWIDTH, 0, SCREENWIDTH, SCREENHEIGHT);
    [self addChildViewController:trendsVC];
    [self.scrollView addSubview:trendsVC.view];
   
   */
   
    ChildDetailedViewController *detailedVC = [[ChildDetailedViewController alloc] init];
    NSString *string = [NSString stringWithFormat:@"http://api.fit-time.cn/client/p%@.html", self.videoModel.ID];
    
    detailedVC.urlString = string;
    detailedVC.view.frame = CGRectMake(SCREENWIDTH, 0, SCREENWIDTH, SCREENHEIGHT);
    
    [self addChildViewController:detailedVC];
    [self.scrollView addSubview:detailedVC.view];
    
}


#pragma mark - view的关联
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    [UIView transitionWithView:self.longView duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
        int num = self.scrollView.contentOffset.x / SCREENWIDTH;
        
        if (num == 0) {
            self.longView.frame = CGRectMake((SCREENWIDTH / 2 - SCREENWIDTH / 3) / 2, 37, SCREENWIDTH / 3, 3);
        } else {
             self.longView.frame = CGRectMake(SCREENWIDTH / 3 +(SCREENWIDTH / 2 - SCREENWIDTH / 3) / 2 * 3, 37, SCREENWIDTH / 3, 3);
        }

    } completion:^(BOOL finished) {
    }];
}


#pragma mark - 头视图效果

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
   
    CGFloat yOffset = scrollView.contentOffset.y;

    if (yOffset > -100) {
        self.tableView.contentInset = UIEdgeInsetsMake(100, 0, 0, 0);
        self.topicLable.text = self.topicSring;
        self.titleLable.hidden = YES;
        self.toolLable.hidden = YES;
        self.trainLable.hidden = YES;
        self.waterView.hidden = YES;
        self.difficultLable.hidden = YES;
        self.headImageView.hidden = YES;
        
//        self.titleLable.alpha = 0;
        
    }
    if (yOffset < -100 ){
        self.tableView.contentInset = UIEdgeInsetsMake(SCREENHEIGHT / 3, 0, 0, 0);
        self.topicLable.text = @"";
        self.titleLable.hidden = NO;
        self.toolLable.hidden = NO;
        self.trainLable.hidden = NO;
        self.waterView.hidden = NO;
        self.difficultLable.hidden = NO;
        self.headImageView.hidden = NO;
    }

    CGFloat alpha = (yOffset + 100) / 10;
    [self.navigationController.navigationBar setBackgroundImage:[self imageWithColor:[[UIColor lightGrayColor]colorWithAlphaComponent:alpha]]forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBarHidden = NO;
}

- (UIImage *)imageWithColor:(UIColor *)color {
    // 描述矩形
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    //    开启位图上下文
    UIGraphicsBeginImageContext(rect.size);
    //    获取位图上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    //    使用color演示填充上下文
    CGContextSetFillColorWithColor(context, [color CGColor]);
    //    渲染上下文
    CGContextFillRect(context, rect);
    //    从上下文中获取图片
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    //    结束上下文
    UIGraphicsEndImageContext();
    
    return theImage;
    
}



#pragma mark - 赋值
- (void)createFuZhi {
    NSString *photoString = [NSString stringWithFormat:@"http://ft-video.fit-time.cn/program_%@.png@!480", _videoModel.ID];
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:photoString]];
    
    NSMutableString *orderString = [NSMutableString stringWithFormat:@"%d", _videoModel.subtitle.intValue];
    NSString *playCount = [NSString stringWithFormat:@"%ld", (long)_videoModel.playCount];
    self.trainLable.text = [NSString stringWithFormat:@"%@次  %@人训练过", orderString, playCount];
    
    //器械
    if (_videoModel.instrument.length == 0) {
        self.toolLable.text = @"无器械";
    } else {
        self.toolLable.text = [NSString stringWithFormat:@"%@", _videoModel.instrument];
    }
    
    for (int i = 0; i < _videoModel.difficulty; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake( 20 * i, 0, 20, 20)];
        imageView.image = [UIImage imageNamed:@"water_b"];
        [self.waterView addSubview:imageView];
    }
    for (int i = (int)_videoModel.difficulty; i < 3 ; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake( 20 * i, 0, 20, 20)];
        imageView.image = [UIImage imageNamed:@"water_w"];
        [self.waterView addSubview:imageView];
    }
    
    //难度
    self.difficultLable.text = @"难度";
    self.topicSring = _videoModel.title;
    self.titleLable.text = _videoModel.title;
}

#pragma mark - 返回
- (void)leftAction:(UIBarButtonItem *)left {
    
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
